﻿Public Class fKalkNaklForm

    Private Sub fKalkNaklForm_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose() 'release all form resources onclose
        If IsNothing(arrcbox01) = False Then 'clears all public arrays
            Erase arrLab01, arrLab02, arrLab03, arrLab04, _
                arrLab05, arrcbox01, arrcbox02, arrtbox01, _
                arrbtnRem01, arrPolozky
        End If
        pocKlikPlus = 1 'resets counter (!IMPORTANT!)

    End Sub


    Private Sub fKalkNaklForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DtbDataSet.tab_kalkulace' table. You can move, or remove it, as needed.
        Me.Tab_kalkulaceTableAdapter.Fill(Me.DtbDataSet.tab_kalkulace)
        Me.Tab_vydajTableAdapter.Fill(Me.DtbDataSet.tab_vydaj)
        Me.Seznam_vyrobkyTableAdapter1.Fill(Me.DtbDataSet.seznam_vyrobky)
        Me.Seznam_vstupyTableAdapter.Fill(Me.DtbDataSet.seznam_vstupy)



        Fill_NaklKalk_cboxCenaKus(Me.DtbDataSet.tab_vydaj, cboxPolCenaKus00, cboxPolJmeno00)
        '----
        If pocKlikPlus < 10 Then
            pocKlikPlusStr = "0" & pocKlikPlus.ToString
        Else
            pocKlikPlusStr = pocKlikPlus.ToString
        End If
        '----
        TabulkaVydaju = Me.DtbDataSet.tab_vydaj
        TabulkaVstupy = Me.DtbDataSet.seznam_vstupy

        
    End Sub

    Private Sub btnZrusit_Click(sender As System.Object, e As System.EventArgs) Handles btnZrusit.Click
        Me.Dispose()
        Me.Close()
        My.Forms.fMain.lblTStrip1.Text = "Akce byla zrušena uživatelem!"
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cboxPolJmeno01_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboxPolJmeno00.SelectedIndexChanged
        Fill_NaklKalk_cboxCenaKus(Me.DtbDataSet.tab_vydaj, cboxPolCenaKus00, cboxPolJmeno00)
    End Sub

    Private Sub btnPolAdd00_Click(sender As System.Object, e As System.EventArgs) Handles btnPolAdd00.Click
        'check of filling previous row, otherwise exits sub - no row is added
        If pocKlikPlus > 1 Then
            If arrcbox01(pocKlikPlus - 2).SelectedIndex.ToString = "" _
                Or arrcbox02(pocKlikPlus - 2).SelectedIndex.ToString = "" _
                Or arrtbox01(pocKlikPlus - 2).Text = "" Then
                MsgBox("Položky řádku nebyly správně vyplněny!", MsgBoxStyle.Critical, "Chyba!")
                Exit Sub
            End If
        End If

        If Me.tboxPolMnozstvi00.Text <> "" Then 'check if tbox of first row was filled
            ReDim Preserve arrLab01(pocKlikPlus - 1)
            ReDim Preserve arrLab02(pocKlikPlus - 1)
            ReDim Preserve arrLab03(pocKlikPlus - 1)
            ReDim Preserve arrLab04(pocKlikPlus - 1)
            ReDim Preserve arrLab05(pocKlikPlus - 1)
            ReDim Preserve arrcbox01(pocKlikPlus - 1)
            ReDim Preserve arrcbox02(pocKlikPlus - 1)
            ReDim Preserve arrtbox01(pocKlikPlus - 1)
            ReDim Preserve arrbtnRem01(pocKlikPlus - 1)
            '---
            If pocKlikPlus < 10 Then
                pocKlikPlusStr = "0" & pocKlikPlus.ToString
            Else
                pocKlikPlusStr = pocKlikPlus.ToString
            End If
            '---
            '1st label
            arrLab01(pocKlikPlus - 1) = New Label()
            With arrLab01(pocKlikPlus - 1)
                .Name = "lblPolJmeno" & pocKlikPlusStr
                .Size = New Size(lblPolJmeno00.Size)
                .Location = New Point(lblPolJmeno00.Location.X, lblPolJmeno00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .Text = "Položka č. " & Convert.ToString(pocKlikPlus + 1) & ":"
                .AutoSize = True
            End With
            Me.Controls.Add(arrLab01(pocKlikPlus - 1))

            '2nd label
            arrLab02(pocKlikPlus - 1) = New Label()
            With arrLab02(pocKlikPlus - 1)
                .Name = "lblPolCenaKus" & pocKlikPlusStr
                .Size = New Size(lblPolCenaKus00.Size)
                .Location = New Point(lblPolCenaKus00.Location.X, lblPolCenaKus00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .Text = lblPolCenaKus00.Text
                .AutoSize = True
            End With
            Me.Controls.Add(arrLab02(pocKlikPlus - 1))

            '3rd label
            arrLab03(pocKlikPlus - 1) = New Label()
            With arrLab03(pocKlikPlus - 1)
                .Name = "lblPolMnozstvi" & pocKlikPlusStr
                .Size = New Size(lblPolMnozstvi00.Size)
                .Location = New Point(lblPolMnozstvi00.Location.X, lblPolMnozstvi00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .Text = lblPolMnozstvi00.Text
                .AutoSize = True
            End With
            Me.Controls.Add(arrLab03(pocKlikPlus - 1))

            '4th label
            arrLab04(pocKlikPlus - 1) = New Label()
            With arrLab04(pocKlikPlus - 1)
                .Name = "lblPolNaklKus" & pocKlikPlusStr
                .Size = New Size(lblPolNaklKus00.Size)
                .Location = New Point(lblPolNaklKus00.Location.X, lblPolNaklKus00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .Text = lblPolNaklKus00.Text
                .AutoSize = True
            End With
            Me.Controls.Add(arrLab04(pocKlikPlus - 1))

            '5th label
            arrLab05(pocKlikPlus - 1) = New Label()
            With arrLab05(pocKlikPlus - 1)
                .Name = "lblPolNaklKusShow" & pocKlikPlusStr
                .Size = New Size(lblPolNaklKusShow00.Size)
                .Location = New Point(lblPolNaklKusShow00.Location.X, lblPolNaklKusShow00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .AutoSize = True
                .ForeColor = lblPolNaklKusShow00.ForeColor
                .Font = lblPolNaklKusShow00.Font
            End With
            Me.Controls.Add(arrLab05(pocKlikPlus - 1))
            'add handler for event "text-changed"
            AddHandler arrLab05(pocKlikPlus - 1).TextChanged, AddressOf arrLab05_TextChanged

            '1st combobox
            arrcbox01(pocKlikPlus - 1) = New ComboBox
            With arrcbox01(pocKlikPlus - 1)
                .Name = "cboxPolJmeno" & pocKlikPlusStr
                .Size = New Size(cboxPolJmeno00.Size)
                .Location = New Point(cboxPolJmeno00.Location.X, cboxPolJmeno00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .DropDownStyle = ComboBoxStyle.DropDownList
                '.DataSource = Me.DtbDataSet.seznam_vstupy 'IMPORTANT: Can't use this approach, since
                '.DisplayMember = "nazev"                              data are bound to one specific binding
                '.ValueMember = "ID"                                   and would change in all cboxes
            End With
            Me.Controls.Add(arrcbox01(pocKlikPlus - 1))
            For i As Long = 0 To Me.DtbDataSet.seznam_vstupy.Rows.Count() - 1
                arrcbox01(pocKlikPlus - 1).Items.Add(Me.DtbDataSet.seznam_vstupy.Rows.Item(i).Item(1))
            Next i
            If arrcbox01(pocKlikPlus - 1).Items.Count > 0 Then
                arrcbox01(pocKlikPlus - 1).SelectedItem = arrcbox01(pocKlikPlus - 1).Items(0)
            End If
            'enables event handling: sub from mod01.vb is called, after item from this control's list is selected
            AddHandler arrcbox01(pocKlikPlus - 1).SelectedIndexChanged, AddressOf arrcbox01_SelectedIndexChanged

            '2nd combobox
            arrcbox02(pocKlikPlus - 1) = New ComboBox()
            With arrcbox02(pocKlikPlus - 1)
                .Name = "cboxPolCenaKus" & pocKlikPlusStr
                .Size = New Size(cboxPolCenaKus00.Size)
                .Location = New Point(cboxPolCenaKus00.Location.X, cboxPolCenaKus00.Location.Y + 30 * pocKlikPlus)
                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                .DropDownStyle = ComboBoxStyle.DropDownList
            End With
            Me.Controls.Add(arrcbox02(pocKlikPlus - 1))

            '1st textbox
            arrtbox01(pocKlikPlus - 1) = New TextBox()
            With arrtbox01(pocKlikPlus - 1)
                .Name = "tboxPolMnozstvi" & pocKlikPlusStr
                .Size = New Size(tboxPolMnozstvi00.Size)
                .Location = New Point(tboxPolMnozstvi00.Location.X, tboxPolMnozstvi00.Location.Y + 30 * pocKlikPlus)
            End With
            Me.Controls.Add(arrtbox01(pocKlikPlus - 1))
            'adding handler for "text-changed" event
            AddHandler arrtbox01(pocKlikPlus - 1).TextChanged, AddressOf arrtbox_TextChanged
            

            'adds button for removing corresponding "row" of controls
            'arrbtnRem01(pocKlikPlus - 1) = New Button()
            'With arrbtnRem01(pocKlikPlus - 1)
            '    .Name = "btnPolOdstranit" & pocKlikPlusStr
            '    .Size = New Size(btnPolAdd00.Size)
            '    .Location = New Point(btnPolAdd00.Location.X, btnPolAdd00.Location.Y + 30)
            '    .Anchor = AnchorStyles.Top Or AnchorStyles.Left
            '    .Font = btnPolAdd00.Font
            '    .ForeColor = Color.OrangeRed
            '    .BackColor = Color.White
            '    .FlatStyle = FlatStyle.Flat
            '    .Text = "X"
            'End With
            'Me.Controls.Add(arrbtnRem01(pocKlikPlus - 1))
            ''adding handler for "click" event
            'AddHandler arrbtnRem01(pocKlikPlus - 1).Click, AddressOf arrbtnRem_Click

            '------
            Me.btnUlozit.Location = New Point(btnUlozit.Location.X, btnUlozit.Location.Y + 30)
            Me.btnZrusit.Location = New Point(btnZrusit.Location.X, btnZrusit.Location.Y + 30)
            Me.btnPolAdd00.Location = New Point(btnPolAdd00.Location.X, btnPolAdd00.Location.Y + 30)
            Me.lblCelkem.Location = New Point(lblCelkem.Location.X, lblCelkem.Location.Y + 30)
            Me.tboxCelkemShow.Location = New Point(tboxCelkemShow.Location.X, tboxCelkemShow.Location.Y + 30)
            '------
            pocKlikPlus += 1
            '------
        Else
            MsgBox("Je třeba vyplnit všechny položky řádku!", MsgBoxStyle.Critical, "Chyba!")
        End If

    End Sub

    Private Sub btnUlozit_Click(sender As System.Object, e As System.EventArgs) Handles btnUlozit.Click
        Dim vyrobekRowView As DataRowView = My.Forms.fMain.cboxVybratVyrobek.SelectedItem
        Dim vstupRowView As DataRowView = Me.cboxPolJmeno00.SelectedItem
        Dim vstupID As Long = vstupRowView.Item(0)
        Dim vyrobekJmeno As String = vyrobekRowView.Item(1)
        Dim vyrobekID As Long = vyrobekRowView.Item(0)
        Dim pocRadku As Long = Me.DtbDataSet.tab_kalkulace.Rows.Count()
        Dim table As DataTable = Me.DtbDataSet.tab_kalkulace
        Dim kalkExistuje As Boolean = False

        If pocRadku = 0 Then
            _lastID = 0
        Else 'if table isn't empty
            _lastID = table.Rows.Item(pocRadku - 1).Item(0) 'get value of last ID and
            For i As Long = 0 To table.Rows.Count() - 1 'check if for selected product corresponding calculation exists in the table
                If table.Rows.Item(i).Item(1) = vyrobekID Then
                    kalkExistuje = True 'if so, then true
                    Exit For
                Else
                    kalkExistuje = False 'else false
                End If
            Next i
        End If
        Try

            If kalkExistuje = True Then
                'table exists, show overwrite confirmation dialog
                My.Forms.fKalkNaklPotvrzeni.ShowDialog()
                'if OK, then
                If My.Forms.fKalkNaklPotvrzeni.DialogResult = Windows.Forms.DialogResult.OK Then
                    'clear old record rows in "tab_kalkulace"
                    For i As Long = 0 To table.Rows.Count() - 1
                        If table.Rows.Item(i).Item(1) = vyrobekID Then
                            table.Rows.Item(i).Delete()
                        Else
                            Continue For
                        End If
                    Next i
                    Me.Tab_kalkulaceTableAdapter.Update(Me.DtbDataSet.tab_kalkulace)

                    'save first "row" values made in designer
                    pocRadku = table.Rows.Count()
                    If pocRadku = 0 Then
                        _lastID = 0
                    Else
                        _lastID = table.Rows.Item(pocRadku - 1).Item(0)
                    End If

                    Dim row As DataRow = table.NewRow()
                    row(0) = _lastID + 1
                    row(1) = vyrobekID
                    row(2) = vstupID
                    row(3) = Convert.ToDecimal(cboxPolCenaKus00.SelectedItem.ToString)
                    row(4) = Convert.ToDecimal(tboxPolMnozstvi00.Text.Trim)
                    table.Rows.Add(row)
                    Me.Tab_kalkulaceTableAdapter.Update(Me.DtbDataSet.tab_kalkulace)
                    'first "row" saved
                    'now save dynamically generated "rows", if created
                    If IsNothing(arrcbox01) = False Then
                        For i As Long = 0 To arrcbox01.GetUpperBound(0)
                            Dim tabVyrobku As DataTable = Me.DtbDataSet.seznam_vyrobky
                            Dim tabVstupu As DataTable = Me.DtbDataSet.seznam_vstupy
                            Dim vstupJmeno As String = arrcbox01(i).SelectedItem.ToString
                            Dim rowDynm As DataRow = table.NewRow()
                            Dim pocRadku2 As Long = table.Rows.Count()
                            _lastID = table.Rows.Item(pocRadku2 - 1).Item(0)

                            rowDynm(0) = _lastID + 1
                            rowDynm(1) = vyrobekID

                            For ii As Long = 0 To tabVstupu.Rows.Count() - 1
                                If tabVstupu.Rows.Item(ii).Item(1) = vstupJmeno Then
                                    rowDynm(2) = tabVstupu.Rows.Item(ii).Item(0)
                                    Exit For
                                Else
                                    Continue For
                                End If
                            Next ii
                            rowDynm(3) = Convert.ToDecimal(arrcbox02(i).SelectedItem.ToString)
                            rowDynm(4) = Convert.ToDecimal(arrtbox01(i).Text.Trim)
                            table.Rows.Add(rowDynm)
                            Me.Tab_kalkulaceTableAdapter.Update(Me.DtbDataSet.tab_kalkulace)
                        Next i
                    End If
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                    My.Forms.fMain.lblTStrip1.Text = "Kalkulace byla úspěšně uložena!"

                End If
            Else
                'save first "row" values made in designer
                Dim row As DataRow = table.NewRow()
                row(0) = _lastID + 1
                row(1) = vyrobekID
                row(2) = vstupID
                row(3) = Convert.ToDecimal(cboxPolCenaKus00.SelectedItem.ToString)
                row(4) = Convert.ToDecimal(tboxPolMnozstvi00.Text.Trim)
                table.Rows.Add(row)
                Me.Tab_kalkulaceTableAdapter.Update(Me.DtbDataSet.tab_kalkulace)
                'first "row" saved
                'now save dynamically generated "rows", if created
                If IsNothing(arrcbox01) = False Then
                    For i As Long = 0 To arrcbox01.GetUpperBound(0)
                        Dim tabVyrobku As DataTable = Me.DtbDataSet.seznam_vyrobky
                        Dim tabVstupu As DataTable = Me.DtbDataSet.seznam_vstupy
                        Dim vstupJmeno As String = arrcbox01(i).SelectedItem.ToString
                        Dim rowDynm As DataRow = table.NewRow()
                        Dim pocRadku2 As Long = table.Rows.Count()
                        _lastID = table.Rows.Item(pocRadku2 - 1).Item(0)

                        rowDynm(0) = _lastID + 1
                        rowDynm(1) = vyrobekID

                        For ii As Long = 0 To tabVstupu.Rows.Count() - 1
                            If tabVstupu.Rows.Item(ii).Item(1) = vstupJmeno Then
                                rowDynm(2) = tabVstupu.Rows.Item(ii).Item(0)
                                Exit For
                            Else
                                Continue For
                            End If
                        Next ii
                        rowDynm(3) = Convert.ToDecimal(arrcbox02(i).SelectedItem.ToString)
                        rowDynm(4) = Convert.ToDecimal(arrtbox01(i).Text.Trim)
                        table.Rows.Add(rowDynm)
                        Me.Tab_kalkulaceTableAdapter.Update(Me.DtbDataSet.tab_kalkulace)
                    Next i
                End If
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
                My.Forms.fMain.lblTStrip1.Text = "Kalkulace byla úspěšně uložena!"
                'My.Forms.fMain.TabPage3.Refresh()
                FillCbox_KalkNaklShow(Me.DtbDataSet.tab_kalkulace, Me.DtbDataSet.seznam_vyrobky, _
                                      My.Forms.fMain.cboxZvolitKalkVyrShow)

            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba!")
            Me.Close()
            Exit Sub
        End Try
    End Sub

    Private Sub tboxPolMnozstvi00_TextChanged(sender As Object, e As System.EventArgs) Handles tboxPolMnozstvi00.TextChanged
        If Me.tboxPolMnozstvi00.Text.Length > 0 Then
            If Decimal.TryParse(Me.tboxPolMnozstvi00.Text.Trim, Decimal.Zero) = True Then
                Me.lblPolNaklKusShow00.Text = _
                    Convert.ToDecimal(tboxPolMnozstvi00.Text.Trim) * _
                    Convert.ToDecimal(cboxPolCenaKus00.SelectedItem)
            Else
                MsgBox("Desetinné číslo nebylo zadáno ve správném tvaru!", MsgBoxStyle.Critical, "Chyba!")
            End If
        End If
    End Sub

    Private Sub lblPolNaklKusShow00_TextChanged(sender As Object, e As System.EventArgs) Handles lblPolNaklKusShow00.TextChanged
        If lblPolNaklKusShow00.Text.Length > 0 Then
            Me.tboxCelkemShow.Text = lblPolNaklKusShow00.Text
        End If
    End Sub
End Class